package com.go.where.family.component.impl;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.go.where.family.component.CustomerReportComponent;
import com.go.where.family.constants.TransmissionCode;
import com.go.where.family.exception.ServerDisponseException;
import com.go.where.family.pojo.CustomerReportInfo;
import com.go.where.family.scoket.Head;
import com.go.where.family.service.CustomerReportService;
import com.go.where.family.utils.CommonUtil;
import com.go.where.family.vo.CustomerReportVo;
import com.google.gson.Gson;

@Component("customerReportComponent")
public class CustomerReportComponentImpl implements CustomerReportComponent {

	private static final Logger log = Logger
			.getLogger(CustomerReportComponentImpl.class);
	private Gson gson;
	@Resource(name = "customerReportService")
	private CustomerReportService customerReportService;

	public CustomerReportComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public boolean save(String body, Head head) {
		CustomerReportVo vo = gson.fromJson(body, CustomerReportVo.class);
		if (vo != null) {
			CustomerReportInfo rep = new CustomerReportInfo();
			rep.setCustomerId(vo.getCustomerId());
			rep.setBigClassId(vo.getBigClassId());
			rep.setSmallClassId(vo.getSmallClassId());
			rep.setCustomerNick(vo.getCustomerNick());
			rep.setReportContent(vo.getReportContent());
			rep.setMobile(vo.getMobile());
			rep.setReportType(vo.getReportType());
			rep.setReportTitle(vo.getReportTitlte());
			rep.setActivityId(vo.getActivityId());
			int result = customerReportService.save(rep);
			if (result > 0) {
				return true;
			} else {
				return false;
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",保存客户举报信息时，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
